package org.jeecg.modules.sistersophie.facade.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sistersophie.entity.CourseInfo;
import org.jeecg.modules.sistersophie.entity.StudentInfo;
import org.jeecg.modules.sistersophie.entity.TeacherInfo;
import org.jeecg.modules.sistersophie.facade.CourseInfoFacade;
import org.jeecg.modules.sistersophie.facade.TeacherInfoFacade;
import org.jeecg.modules.sistersophie.model.vo.TeacherSearchVo;
import org.jeecg.modules.sistersophie.service.ITeacherInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherInfoFacadeImpl implements TeacherInfoFacade {

    @Autowired
    private ITeacherInfoService teacherInfoService;

    @Autowired
    private CourseInfoFacade courseInfoFacade;


    @Transactional
    public boolean save(TeacherInfo teacherInfo) {
        teacherInfo.setIsDelete(0);
        if(StringUtils.isNotBlank(teacherInfo.getPhone()) && StringUtils.isNotBlank(teacherInfo.getPhonePrefix())){
            teacherInfo.setPhone(teacherInfo.getPhonePrefix()+"-" + teacherInfo.getPhone());
        }
        TeacherInfo qteacher = new TeacherInfo();
        qteacher.setFirstName(teacherInfo.getFirstName());
        qteacher.setLastName(teacherInfo.getLastName());
        qteacher.setIsDelete(0);
        //检查名称唯一性
        QueryWrapper<TeacherInfo> queryWrapper = QueryGenerator.initQueryWrapper(qteacher, new HashMap<>());
        List<TeacherInfo> teacherInfos = teacherInfoService.list(queryWrapper);
        if (teacherInfos != null && teacherInfos.size() > 0) {
            throw new RuntimeException(teacherInfo.getFirstName() + " " + teacherInfo.getLastName() + " already exists, please re-enter");
        }
        qteacher = new TeacherInfo();
        qteacher.setEmail(teacherInfo.getEmail());
        qteacher.setIsDelete(0);
        teacherInfos = teacherInfoService.list(queryWrapper);
        if (teacherInfos != null && teacherInfos.size() > 0) {
            throw new RuntimeException(teacherInfo.getEmail() + " already exists, please re-enter");
        }

        qteacher = new TeacherInfo();
        qteacher.setPhone(teacherInfo.getPhone());
        qteacher.setIsDelete(0);
        teacherInfos = teacherInfoService.list(queryWrapper);
        if (teacherInfos != null && teacherInfos.size() > 0) {
            throw new RuntimeException(teacherInfo.getPhone() + " already exists, please re-enter");
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        teacherInfo.setCreateName(sysUser.getUsername());
        teacherInfo.setCreateTime(new Date());
        teacherInfo.setUpdateName(sysUser.getUsername());
        teacherInfo.setUpdateTime(new Date());
        teacherInfo.setIsDelete(0);
        teacherInfoService.save(teacherInfo);
        return true;
    }

    @Transactional
    public boolean edit(TeacherInfo teacherInfo) {
        if(StringUtils.isNotBlank(teacherInfo.getPhone()) && StringUtils.isNotBlank(teacherInfo.getPhonePrefix())){
            teacherInfo.setPhone(teacherInfo.getPhonePrefix()+"-" + teacherInfo.getPhone());
        }
        List<TeacherInfo> info =  teacherInfoService.selectByNameNotIn(teacherInfo.getFirstName(),teacherInfo.getLastName(),teacherInfo.getId());
        if (CollectionUtils.isNotEmpty(info)) {
            throw new RuntimeException(teacherInfo.getFirstName() + " " + teacherInfo.getLastName() + " already exists, please re-enter");
        }
        info =  teacherInfoService.selectByEmailNotIn(teacherInfo.getEmail(),teacherInfo.getId());
        if (CollectionUtils.isNotEmpty(info)) {
            throw new RuntimeException(teacherInfo.getEmail() +  " already exists, please re-enter");
        }
        info =  teacherInfoService.selectByPhoneNotIn(teacherInfo.getPhone(),teacherInfo.getId());
        if (CollectionUtils.isNotEmpty(info)) {
            throw new RuntimeException(teacherInfo.getPhone() +  " already exists, please re-enter");
        }
        teacherInfoService.updateById(teacherInfo);
        return true;
    }

    public void deleteById(String id){
        //老师已关联课程后 不可删除
        TeacherInfo teacherInfo = teacherInfoService.getById(id);
        int num = courseInfoFacade.selectTeacherCourseCount(teacherInfo.getId());
        if(num > 0){
            throw new RuntimeException(" The teacher has already associated the course information, this operation is not allowed ");
        }
        teacherInfo.setIsDelete(1);
        teacherInfoService.updateById(teacherInfo);
    }
}